package com.wss.module.wan.ui.wxnumber.adapter;

import com.wss.common.base.adapter.BaseListAdapter;
import com.wss.common.base.adapter.listener.OnListItemClickListener;
import com.wss.common.base.adapter.superadapter.SuperViewHolder;
import com.wss.common.utils.ColorUtils;
import com.wss.module.wan.ResourceTable;
import com.wss.module.wan.bean.Article;
import ohos.agp.components.Text;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.utils.Color;
import ohos.app.Context;
import ohos.global.resource.NotExistException;
import ohos.global.resource.WrongTypeException;
import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.util.List;

/**
 * Describe：公众号文章适配器
 * Created by 吴天强 on 2018/11/15.
 */
public class WXArticleAdapter extends BaseListAdapter<Article> {

    private Context context;

    public WXArticleAdapter(Context context, List<Article> items, OnListItemClickListener<Article> listener) {
        super(context, items, ResourceTable.Layout_wan_item_of_wx_article_list, listener);
        this.context = context;
    }

    private int getColor(int res) {
        try {
            return context.getResourceManager().getElement(res).getColor();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (NotExistException e) {
            e.printStackTrace();
        } catch (WrongTypeException e) {
            e.printStackTrace();
        }
        return Color.BLACK.getValue();
    }

    @Override
    public void onBindData(@NotNull SuperViewHolder holder, int layoutPosition, @NotNull Article data) {
        Text textView = holder.findViewById(ResourceTable.Id_tv_text);
        if (layoutPosition % 2 == 0) {
            ShapeElement element = new ShapeElement();
            element.setRgbColor(ColorUtils.
                    colorToRgbColor(getColor(ResourceTable.Color_light_blue)));
            textView.setBackground(element);
        } else {
            ShapeElement element = new ShapeElement();
            element.setRgbColor(ColorUtils.
                    colorToRgbColor(getColor(ResourceTable.Color_light_yellow)));
            textView.setBackground(element);
        }
        textView.setText(data.getTitle());
    }
}
